package com.filldream.fastboot.common.okutil.stock.ths.top;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.filldream.fastboot.common.stockUtils.bd.BdUtil;
import java.util.*;

public class ThsTopUtil {

    public static Map<String,String> getUrl(String searchType){
        Map<String,String> result = new HashMap<>();
        result.put("type", "get");
        String url;
        if( searchType.equals("1") || searchType.equals("2") ){
            url = "https://dq.10jqka.com.cn/fuyao/hot_list_data/out/hot_list/v1/plate?type=" + (searchType.equals("1") ? "concept" : "industry" );
        }else if(searchType.equals("3")){
            url = "https://dq.10jqka.com.cn/fuyao/hot_list_data/out/hot_list/v1/topic?page=1&page_size=30";
        }else if(searchType.equals("4")){
            url = "https://ai.iwencai.com/index/urp/getdata/basic?tag=%E5%90%8C%E8%8A%B1%E9%A1%BA%E7%83%AD%E6%A6%9C_%E6%96%B0%E7%83%AD%E6%96%87";
        }else if( searchType.equals("ETF") ){
            url = "https://dq.10jqka.com.cn/fuyao/hot_list_data/out/hot_list/v1/etf";
        }else if( searchType.equals("ETF_TAG") ){
            url = "https://dataq.10jqka.com.cn/dataapi/tagservice/fetch/v1/tag_data";
            result.put("type", "post");
        }else{
            final String typeStr = searchType.equals("tech") || searchType.equals("value") ? "day" : "hour";
            url = "https://dq.10jqka.com.cn/fuyao/hot_list_data/out/hot_list/v1/stock?stock_type=a&type="+typeStr+"&list_type=" + searchType ;
        }
        result.put("url",url);
        return result;
    }


    public static JSONObject getThsTop(String searchType) {
        String url = getUrl(searchType).get("url");
        String resultStr;
        if( getUrl(searchType).get("type").equals("get") ){
            resultStr = HttpUtil.get(url);
        }else{
            resultStr = HttpUtil.post(url, "");
        }
        if( searchType.equals("4") ){
            JSONObject parse = JSONObject.parse(resultStr);
            JSONArray jsonArray = parse.getJSONObject("answer").getJSONArray("components").getJSONObject(0).getJSONObject("data").getJSONArray("datas");
            jsonArray.sort(Comparator.comparingInt(
                    obj -> ((JSONObject)obj).getInteger("rank")
            ));
            JSONObject reuslt = new JSONObject();
            reuslt.put("status_code",0);
            reuslt.put("data",jsonArray);
            return reuslt;
        }else if( searchType.equals("ETF") ){
            JSONObject parse = JSONObject.parse(resultStr);
//            JSONArray jsonArray = parse.getJSONObject("data").getJSONArray("list");
//            JSONObject etfTag = getThsTop("ETF_TAG");
//            JSONArray etfTagArr = etfTag.getJSONArray("data");
//            jsonArray.forEach(t -> {
//                JSONObject tt = (JSONObject) t;
//                String key = tt.getString("market") + "." + tt.getString("code");
//                //查找对应的标签
//                JSONObject tag = new JSONObject();
//                for (int i = 0; i < etfTagArr.size(); i++) {
//                    JSONObject etfTagItem = etfTagArr.getJSONObject(i);
//                    if( etfTagItem.getString("code").equals(key) ){
//                        JSONArray tagTagArr = etfTagItem.getJSONArray("values");
//                        tag.put("popularity_tag", tagTagArr.getJSONObject(0).getJSONArray("show_tag").getString(0));
//                        List<String> tagConceptTag = new ArrayList<>();
//                        for (int j = 1; j < tagTagArr.size(); j++) {
//                            tagConceptTag.add(tagTagArr.getJSONObject(j).getJSONArray("show_tag").getString(0));
//                        }
//                        tag.put("concept_tag",tagConceptTag);
//                    }
//                }
//                tt.put("tag",tag);
//            });
            return parse;
        }else if( searchType.equals("ETF_TAG") ){
            JSONArray etfTag = getEtfTag("{\"code_selectors\":{\"include\":[{\"type\":\"stock_code\",\"values\":[\"36:159699\",\"20:518680\",\"20:588220\",\"20:513350\",\"20:513200\",\"20:588460\",\"20:513120\",\"36:159518\",\"20:512690\",\"36:159567\",\"20:512710\",\"20:588080\",\"36:159588\",\"36:159570\",\"36:159353\",\"20:513090\",\"36:159309\",\"36:159851\",\"36:159869\",\"36:159971\",\"20:513400\",\"36:159740\",\"20:513060\",\"36:159329\",\"36:159937\",\"20:588200\",\"20:510300\",\"20:513010\",\"20:512880\",\"20:562500\",\"20:513050\",\"36:159892\",\"20:513260\",\"36:159981\",\"20:512810\",\"20:512670\",\"20:518850\",\"20:520700\",\"36:159792\",\"36:159819\",\"20:512660\",\"36:159506\",\"20:512010\",\"36:159813\",\"20:513780\",\"20:520500\",\"20:561360\",\"36:159941\",\"20:520830\",\"20:512170\",\"20:513300\",\"36:159992\",\"36:159928\",\"20:513100\",\"36:159949\",\"20:515220\",\"20:510050\",\"20:516860\",\"20:517520\",\"20:512890\",\"20:513770\",\"36:159217\",\"36:159934\",\"20:520600\",\"20:515790\",\"20:512760\",\"36:159822\",\"36:159841\",\"20:512000\",\"20:515880\",\"20:562570\",\"20:516770\",\"36:159995\",\"20:516780\",\"20:563300\",\"20:510210\",\"36:159887\",\"36:159713\",\"20:513980\",\"20:510500\",\"20:513750\",\"36:159766\",\"20:588790\",\"20:516510\",\"20:515080\",\"36:159857\",\"36:159363\",\"20:588760\",\"20:513500\",\"20:513870\",\"36:159985\",\"36:159316\",\"20:513600\",\"20:560090\",\"36:159227\",\"20:518600\",\"20:513160\",\"36:159697\",\"20:588190\",\"20:512800\"]}]},\"tag_infos\":[{\"tag_key\":\"ifund_etf_t0\"},{\"tag_key\":\"ifund_biz_FS34806_tech_side\"},{\"tag_key\":\"ifund_biz_FS34806_fund_side\"}]}");
            JSONObject reuslt = new JSONObject();
            reuslt.put("status_code",0);
            reuslt.put("data",etfTag);
            return reuslt;
        }else if( searchType.equals("bdHotList") ){
            return BdUtil.getHotSearch();
        }
        return JSONObject.parse(resultStr);
    }


    public static JSONArray getEtfTag(String body){
        String url = getUrl("ETF_TAG").get("url");
        String resultStr = HttpUtil.post(url, body);
        JSONObject parse = JSONObject.parse(resultStr);
        return parse.getJSONObject("data").getJSONArray("data");
    }

}
